<?php

class Actualite extends Admin_Controller {

    function Actualite() {
        parent::Admin_Controller();
        $this->load->model('Actualite_m');
    }

    function index() {
        $this->liste_actualite();
    }

    function liste_actualite() {
        is_admin();
        $this->_data['page_title'] = "Liste de actualite";
        $per_page = $this->uri->segment(3) != '' ? $this->uri->segment(3) : 7;
        $off_set = $this->uri->segment(4) != '' ? $this->uri->segment(4) : 0;
        $config['base_url'] = base_url() . 'index.php/actualite/liste_actualite/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '7';

        $config['full_tag_open'] = "<div id='containpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';

        $config['total_rows'] = $this->db->count_all('actu');
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['actualite'] = $this->Actualite_m->get_all_news($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste actualite";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['total'] = $this->db->count_all('actu');
        $this->display_view('actualite/liste_actualite');
    }

    function add_news() {
        is_admin();
        $this->_data['page_title'] = "Ajouter de nouvelles";
        $config = array(
            array(
                'field' => 'txtFrTitle',
                'label' => 'French title',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        $this->form_validation->set_message('required', 'Ban phai nhap title');
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $image_path = './assets/actuimg';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                    $this->_data['error'] = $this->upload->display_errors();
                    $this->display_view('product/add_news');
                } else {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'nomfr' => $this->input->post('txtFrTitle'),
                        'nomen' => $this->input->post('txtEngtitle'),
                        'nomes' => $this->input->post('txtSpainTitle'),
                        'nomal' => $this->input->post('txtGermanTitle'),
                        'nomho' => $this->input->post('txtDutchTitle'),
                        'catid' => $this->input->post('categorie'),
                        'img' => $upload_info['file_name'],
                        'descriptionfr' => $this->input->post('txtFrDes'),
                        'descriptionen' => $this->input->post('txtEnDes'),
                        'descriptiones' => $this->input->post('txtSpDes'),
                        'descriptional' => $this->input->post('txtGeDes'),
                        'descriptionho' => $this->input->post('txtDuctchDes')
                    );
                    $this->Actualite_m->add_news($data);
                    redirect(site_url('actualite/liste_actualite'));
                    //print_r($data);
                }
            } else {
                $this->display_view('actualite/add_news');
            }
        } else {
            $this->display_view('actualite/add_news');
        }
    }

    function update_news($id, $per_page, $off_set) {
        is_admin();
        $this->_data['page_title'] = "Mise à jour nouvelles";
        $config = array(
            array(
                'field' => 'txtFrTitle',
                'label' => 'French title',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        $this->form_validation->set_message('required', 'Ban phai nhap title');
        $this->_data['query'] = $this->Actualite_m->get_news_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $image_path = './assets/actuimg';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 2000
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                    $data = array(
                        'nomfr' => $this->input->post('txtFrTitle'),
                        'nomen' => $this->input->post('txtEngtitle'),
                        'nomes' => $this->input->post('txtSpainTitle'),
                        'nomal' => $this->input->post('txtGermanTitle'),
                        'nomho' => $this->input->post('txtDutchTitle'),
                        'catid' => $this->input->post('categorie'),
                        'descriptionfr' => $this->input->post('txtFrDes'),
                        'descriptionen' => $this->input->post('txtEnDes'),
                        'descriptiones' => $this->input->post('txtSpDes'),
                        'descriptional' => $this->input->post('txtGeDes'),
                        'descriptionho' => $this->input->post('txtDuctchDes')
                    );
                    $this->Actualite_m->update_news($data, $id);
                    redirect(site_url('actualite/liste_actualite/' . $per_page . '/' . $off_set));
                } else {
                    $query = $this->Actualite_m->get_news_by_id($id);
                    if ($query->num_rows() > 0) {
                        $info = $query->row();
                        $image_to_delete = './assets/actuimg/' . $info->img;
                        $thumb_to_delete = './assets/actuimg/thumbs/' . $info->img;
                        delete_image($image_to_delete, $thumb_to_delete);
                    }
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'nomfr' => $this->input->post('txtFrTitle'),
                        'nomen' => $this->input->post('txtEngtitle'),
                        'nomes' => $this->input->post('txtSpainTitle'),
                        'nomal' => $this->input->post('txtGermanTitle'),
                        'nomho' => $this->input->post('txtDutchTitle'),
                        'catid' => $this->input->post('categorie'),
                        'img' => $upload_info['file_name'],
                        'descriptionfr' => $this->input->post('txtFrDes'),
                        'descriptionen' => $this->input->post('txtEnDes'),
                        'descriptiones' => $this->input->post('txtSpDes'),
                        'descriptional' => $this->input->post('txtGeDes'),
                        'descriptionho' => $this->input->post('txtDuctchDes')
                    );
                    $this->Actualite_m->update_news($data, $id);
                    redirect(site_url('actualite/liste_actualite/' . $per_page . '/' . $off_set));
                    //print_r($data);
                }
            } else {
                $this->display_view('actualite/update_news');
            }
        } else {
            $this->display_view('actualite/update_news');
        }
    }

    function delete_news($id, $per_page, $off_set) {
        is_admin();
        $query = $this->Actualite_m->get_news_by_id($id);
        if ($query->num_rows() > 0) {
            $info = $query->row();
            if ($this->Actualite_m->delete_news($id) > 0) {
                $image_to_delete = './assets/actuimg/' . $info->img;
                $thumb_to_delete = './assets/actuimg/thumbs/' . $info->img;
                delete_image($image_to_delete, $thumb_to_delete);
            }
        }
        redirect(site_url('actualite/liste_actualite/' . $per_page . '/' . $off_set));
    }

    //
    function liste_categorie() {
        is_admin();
        $this->_data['page_title'] = "Liste de categorie";
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/actualite/liste_categorie/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '10';
        $config['total_rows'] = $this->db->count_all('catactu');

        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }
        
        $this->_data['catactu']=$this->Actualite_m->get_all_categories($per_page,$off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste categorie";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['total'] = $this->db->count_all('catactu');
        $this->display_view('actualite/liste_actu_categorie');
    }

    function delete_news_cat($id,$per_page,$off_set){
        is_admin();
        $this->Actualite_m->delete_news_cat($id);
       redirect(site_url('actualite/liste_categorie/' . $per_page . '/' . $off_set));
    }

    function add_news_cat(){
        is_admin();
        $this->_data['page_title'] = "Ajouter la catégorie Nouvelles";
        $config = array(
            array(
                'field' => 'frCat',
                'label' => 'French title',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if($this->input->post('submit')){
                if($this->form_validation->run()){
                    $data = array(
                        'nomfr' => $this->input->post('frCat'),
                        'nomen' => $this->input->post('enCat'),
                        'nomes' => $this->input->post('spaCat'),
                        'nomal' => $this->input->post('gerCat'),
                        'nomho' => $this->input->post('duCat')
                       );
                    $this->Actualite_m->add_news_cat($data);
                    redirect(site_url('actualite/liste_categorie'));
                }else{
                    $this->display_view('actualite/add_news_cat');
                }
        }else{
            $this->display_view('actualite/add_news_cat');
        }
    }

    function update_news_cat($id, $per_page, $off_set){
        is_admin();
        $this->_data['page_title'] = "Mise à jour catégorie de nouvelles";
        $config = array(
            array(
                'field' => 'frCat',
                'label' => 'French title',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        $this->_data['query'] = $this->Actualite_m->get_news_cat_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if($this->input->post('submit')){
                if($this->form_validation->run()){
                    $data = array(
                        'nomfr' => $this->input->post('frCat'),
                        'nomen' => $this->input->post('enCat'),
                        'nomes' => $this->input->post('spaCat'),
                        'nomal' => $this->input->post('gerCat'),
                        'nomho' => $this->input->post('duCat')
                       );
                    $this->Actualite_m->update_news_cat($data, $id);
                    redirect(site_url('actualite/liste_categorie/' . $per_page . '/' . $off_set));
                }else{
                    $this->display_view('actualite/update_news_cat');
                }
        }else{
            $this->display_view('actualite/update_news_cat');
        }
    }

}

?>
